// releasePage/pages/addProjectTeam/addMember/addMember.js
import throttle from '../../../../utils/throttle.js';
const app = getApp();
// 是否正在修改input内的值
let changeInputValue = false;
Page({
    /**
     * 页面的初始数据
     */
    data: {
        pageNum: 0,
        loading: true,
        loadingAll: false,
        realnameorphone: '',
        //成员列表
        memberInfo: [],
        index: 0,
    },
    selectMember(e) {
        let content = {};
        let contentIndex;
        let pages = getCurrentPages(); //当前页面栈
        let prevPage = pages[pages.length - 2]; //上一页面
        for (let i = 0; i < prevPage.data.projectTeamMembers.length; i++) {
            if (
                e.currentTarget.dataset.uid ==
                prevPage.data.projectTeamMembers[i].uid
            ) {
                wx.showToast({
                    title: '该成员已添加',
                    icon: 'none', //如果要纯文本，不要icon，将值设为'none'
                    duration: 1500,
                });
                return;
            }
        }
        if (this.data.index == 0) {
            content.title = '交付组长';
            content.realname = e.currentTarget.dataset.name;
            content.uid = e.currentTarget.dataset.uid;
            content.backup = '';
            content.backupid = '';
            contentIndex = 'projectTeamMembers[' + this.data.index + ']';
            prevPage.setData({
                [contentIndex]: content,
            });
        } else if (this.data.index > 0) {
            content.title = '组员' + this.data.index;
            content.realname = e.currentTarget.dataset.name;
            content.uid = e.currentTarget.dataset.uid;
            content.backup = '';
            content.backupid = '';
            contentIndex = 'projectTeamMembers[' + this.data.index + ']';
            prevPage.setData({
                [contentIndex]: content,
            });
        } else {
            content.title = '组员' + prevPage.data.projectTeamMembers.length;
            content.realname = e.currentTarget.dataset.name;
            content.uid = e.currentTarget.dataset.uid;
            content.backup = '';
            content.backupid = '';
            prevPage.data.projectTeamMembers.push(content);
            prevPage.setData({
                projectTeamMembers: prevPage.data.projectTeamMembers,
            });
            // contentIndex = 'projectTeamMembers[' + prevPage.data.projectTeamMembers.length + ']'
        }
        // console.log(prevPage.data.projectTeamMembers)
        wx.navigateBack({
            detal: 1,
        });
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getMemberInfo();
        this.setData({
            index: options.index,
        });
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {},

    search: throttle(function (e) {
        this.setData(
            {
                realnameorphone: e.detail.value,
                pageNum: 0,
                memberInfo: [],
                loadingAll: false,
            },
            () => {
                this.getMemberInfo();
            }
        );
    }, 1000),

    getMemberInfo() {
        let _that = this;
        if (!_that.data.loadingAll) {
            _that.setData({
                pageNum: _that.data.pageNum + 1,
                loading: true,
                loadingAll: false,
            });
            wx.request({
                url:
                    app.globalData.rootPath +
                    `cjMap/getInsideUserByRealNameOrPhone`,
                method: 'GET',
                header: {
                    'content-type': 'application/x-www-form-urlencoded',
                },
                data: {
                    authId: app.globalData.authId,
                    realnameorphone: _that.data.realnameorphone, //模糊查询真实姓名或手机号（不传则查全部）
                    pageSize: 15,
                    pageNo: _that.data.pageNum,
                },
                success: function (res) {
                    console.log(res);
                    if (res.data.status == 0) {
                        for (let i = 0; i < res.data.data.length; i++) {
                            _that.data.memberInfo.push(res.data.data[i]);
                        }
                        _that.setData({
                            memberInfo: _that.data.memberInfo,
                            loading: false,
                        });
                        if (res.data.data.length < 15) {
                            _that.setData({
                                loadingAll: true,
                            });
                        }
                        // if (!_that.data.memberInfo.length) {
                        //     _that.setData({
                        //         loading: false,
                        //         loadingAll: false
                        //     })
                        // }
                    } else {
                        if (res.data.errmsg == '输入页码超出范围') {
                            _that.setData({
                                loadingAll: true,
                                loading: false,
                            });
                        }
                    }
                },
            });
        }
    },
});
